package ssen.mvc.ondisplay {
import ssen.mvc.core.IContextView;
import ssen.mvc.core.IContextViewInjector;
import ssen.mvc.core.IViewCreator;
import ssen.mvc.core.IViewInjector;

public class DisplayViewCreator implements IViewCreator {
	private var viewInjector:IViewInjector;
	private var contextViewInjector:IContextViewInjector;

	public function DisplayViewCreator(viewInjector:IViewInjector, contextViewInjector:IContextViewInjector) {
		this.viewInjector=viewInjector;
		this.contextViewInjector=contextViewInjector;
	}

	public function dispose():void {
		viewInjector=null;
		contextViewInjector=null;
	}

	public function create(viewClass:Class):Object {
		var view:Object=new viewClass();

		if (view is IContextView) {
			var contextView:IContextView=view as IContextView;

			if (!contextView.contextInitialized) {
				contextViewInjector.injectInto(view as IContextView);
			}
		} else if (viewInjector.hasMapping(view)) {
			viewInjector.injectInto(view);
		}

		return view;
	}

}
}
